package com.zlaidyrac.weride.ui;

import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zlaidyrac.weride.main.R;
import com.zlaidyrac.weride.ui.GoogleMapsActivity;
import com.zlaidyrac.weride.ui.MyItemizedOverlay;
import com.zlaidyrac.weride.webclient.WebClient;

public class NewUser extends Activity {
	NewUser activity;
	String fname;
	String lname;
	String username;
	String password;
	String address;
	int role;
	EditText addre;
	String mail;
	String fb;
	String contct;
	double lati = 99.7654d;
	double longi = 79.7654d;

	// SoapObject request;
	// SoapSerializationEnvelope envelope;

	public void setAddres(String address) {
		addre.setText(address);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_newuser);

		Button submit = (Button) findViewById(R.id.button1);
		ImageButton map = (ImageButton) findViewById(R.id.imageButton);
		final EditText firstname = (EditText) findViewById(R.id.fname);
		final EditText uname = (EditText) findViewById(R.id.username);
		final EditText pword = (EditText) findViewById(R.id.password);
		final EditText addre = (EditText) findViewById(R.id.address);
		final Spinner rol = (Spinner) findViewById(R.id.spinner1);
		final EditText email = (EditText) findViewById(R.id.email);
		final EditText facebook = (EditText) findViewById(R.id.facebook);
		final EditText contact = (EditText) findViewById(R.id.contact);

		final HashMap<String,String> params = new HashMap<String,String>();

		map.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(NewUser.this,
						GoogleMapsActivity.class);
				startActivity(intent);

			}
		});

		// New user form Submit onClick Listner
		submit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				fname = firstname.getText().toString();
				username = uname.getText().toString();
				password = pword.getText().toString();
				address = addre.getText().toString();
				role = rol.getSelectedItemPosition();
				mail = email.getText().toString();
				fb = facebook.getText().toString();
				contct = contact.getText().toString();
				params.put("fname", fname);
				params.put("user_name", username);
				params.put("password", password);
				params.put("address", address);
				params.put("gender", new Integer(role).toString());
				params.put("email", mail);
				params.put("fb_url", fb);
				params.put("contact", contct);

				new addUserTask().execute(params);

			}
		});
		activity = this;

	}

	private class addUserTask extends AsyncTask<HashMap<String,String>, Void, Boolean> {

		@Override
		protected Boolean doInBackground(HashMap<String,String>... params) {
			HashMap<String, String> result = WebClient.postRequest("register",
					params[0]);
			if (result.get("message").equals("SUCCESS")) {

				return true;
			} else {

				return false;
			}
		}

		protected void onPostExecute(Boolean success) {
			super.onPostExecute(success);
			if (success) {
				Toast.makeText(getApplicationContext(),
						"Welcome,  new member of WeRide!", Toast.LENGTH_SHORT)
						.show();
				activity.finish();
			} else
				Toast.makeText(getApplicationContext(),
						"Sorry,  could not add you as a new user",
						Toast.LENGTH_SHORT).show();
		}

	}
}
